﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Diplom.NumericArrays;
using Diplom.Utilities;
using Diplom.Graphics.D2D;
using Diplom.Graphics.D3D;
using Diplom.Wavelets;
using Diplom.Forms;
using System.Text.RegularExpressions;
using System.IO;
using System.Threading;

namespace Diplom
{
    public partial class MainForm : Form
    {
        Numeric1DArray array1D = null; // исходный ряд
        Numeric2DArray array2D = null; // после преобразования
        Numeric2DArray skalogramma = null;
        Numeric1DArray skeylogramma = null;
        Numeric2DArray skeleton = null;
        
        System.Drawing.Graphics graphics = null;
        DrawStyle drawStyle = null;

        public MainForm()
        {
            InitializeComponent();
        }

        #region old
        //private void button1_Click(object sender, EventArgs e)
        //{
        //    //button1.Text = "Running";

        //    Numeric1DArray array = null; //Можно и null передавать по реффу, все равно переприсваивается =)
        //    Numeric1DArray array2 = null;
        //    Numeric1DArray array3 = null;

        //    //ArrayGenerator.Generate(Function.Sin, ref array, new Parameters(100, 1, 0, 1.0, 0, 5 * Math.PI));
        //    //ArrayGenerator.Generate(Function.Sin, ref array2, new Parameters(100, 1, 0, 4.0, 0, 3 * Math.PI));
        //    ArrayGenerator.Generate(Function.Sin, ref array, new Parameters(200, 3, 0, 2.0, 0, 7.5 * Math.PI));
        //    //ArrayGenerator.Generate(Function.Cos, ref array3, new Parameters(100, 1, 0, 2.0, 0, 2.5 * Math.PI));
        //    //array = array.Concat(ref array3);
        //    ArrayGenerator.Generate(Function.Noise, ref array2, new Parameters(40, 1.0, 0, 0.5, 0, 2.5 * Math.PI));

        //    //array = array.Concat(ref array2);               
        //    ArrayGenerator.SumArrays(ref array, ref array2, 75);
        //    Image img = pictureBox1.Image;
        //    System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(img);

        //    DrawStyle style = new DrawStyle(Color.Blue, new Point(0, 0), new Point(1, 1), Color.Red, Color.Red);

        //    //Numeric2DArray wave = HAAR.Transform(ref array);
        //    Numeric2DArray wave = MHAT.Transform(ref array);
        //    Filter.SimpleDenoise(ref wave, array.Length, array.Dispersion);
        //    //array2 = HAAR.Syntes(ref wave, array.Length);
        //    array2 = MHAT.Syntes(ref wave, array.Length);

        //    // Periodogramma
        //    /*Complex1DArray comp = Fourier.Transform(ref array);
        //    Numeric1DArray res = Analitics.Periodogramma(ref comp);


        //    Numeric2DArray wave = MHAT.Analys(ref array, 250, 450,true,false);
        //    Numeric2DArray scal = Analitics.Scalogramma(ref wave);
        //    Numeric2DArray scel = Analitics.Sceleton(ref scal);
        //    Numeric1DArray scey = Analitics.Sceylogramma(ref scal, true);*/

        //    /*Graphics.D3D.ColorVisualizerNonStatic col = new Diplom.Graphics.D3D.ColorVisualizerNonStatic(52);
        //    col.DrawGradientPaint(ref gr, ref scal, new RectangleF(new PointF(125, 125), new SizeF(pictureBox1.Width * 1.5F / 2 - 50, pictureBox1.Height * 1.5F / 2 - 100)));
        //    Graphics.D3D.IsolineVisualizer.DrawSignature(ref gr, ref scal, new RectangleF(new PointF(125, 125), new SizeF(pictureBox1.Width * 1.5F / 2 - 50, pictureBox1.Height * 1.5F / 2 - 100)), style, new Size(20, 15));
        //    */

        //    //Graphics.D3D.IsolineVisualizer.DrawIsoline(ref gr, new RectangleF(new PointF(125, 125), new SizeF(pictureBox1.Width * 1.5F / 2 - 50, pictureBox1.Height * 1.5F / 2 - 100)), ref wave, 52);
        //    //Graphics.D2D.D2DVisualizer.DrawGraphic(ref gr, ref array, new RectangleF(new PointF(125, 25), new SizeF(pictureBox1.Width * 1.0F / 2 - 50, pictureBox1.Height * 1.0F / 2 - 100)), style);
        //    Graphics.D2D.D2DVisualizer.DrawGraphic(ref gr, ref array, new RectangleF(new PointF(125, 25), new SizeF(pictureBox1.Width * 1.0F / 2 - 50, pictureBox1.Height * 1.0F / 2 - 100)), style);
        //    Graphics.D2D.D2DVisualizer.DrawSignature(ref gr, ref array, new RectangleF(new PointF(125, 25), new SizeF(pictureBox1.Width * 1.0F / 2 - 50, pictureBox1.Height * 1.0F / 2 - 100)), style,new Size(20,10));
        //    Graphics.D2D.D2DVisualizer.DrawGraphic(ref gr, ref array2, new RectangleF(new PointF(125, 25 + pictureBox1.Height * 1.0F / 2), new SizeF(pictureBox1.Width * 1.0F / 2 - 50, pictureBox1.Height * 1.0F / 2 - 100)), style);
        //    Graphics.D2D.D2DVisualizer.DrawSignature(ref gr, ref array2, new RectangleF(new PointF(125, 25 + pictureBox1.Height * 1.0F / 2), new SizeF(pictureBox1.Width * 1.0F / 2 - 50, pictureBox1.Height * 1.0F / 2 - 100)), style, new Size(20, 10));

        //    img.Save("res.Jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
        //    pictureBox1.Image = img;

        //} 
        #endregion

        #region old2
        // private void button2_Click(object sender, EventArgs e)
        // {
        //     Numeric1DArray array = null;
        //     ArrayGenerator.Generate(Function.Sin, ref array, new Parameters(100, 1, 0, 2.0, 0, 2.5 * Math.PI));
        //     Numeric1DArray array2 = null;
        //     ArrayGenerator.Generate(Function.Cos, ref array2, new Parameters(100, 5, 0, 2.0, 0, 2.5 * Math.PI));
        //     array = array.Concat(ref array2);

        //     Image img = pictureBox1.Image;
        //     System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(img);
        //     DrawStyle style = new DrawStyle(Color.Blue, new Point(0, 0), new Point(1, 1), Color.Red, Color.Green);

        //     D2DVisualizer.DrawGraphic(ref gr, ref array, new RectangleF(new PointF(25, 50), new SizeF(pictureBox1.Width - 50, pictureBox1.Height/2 - 100)), style);
        //     D2DVisualizer.DrawSignature(ref gr, ref array, new RectangleF(new PointF(25, 50), new SizeF(pictureBox1.Width - 50, pictureBox1.Height/2 - 100)), style, new Size(25, 15));

        //     int len = array.Length;
        //     Numeric2DArray wave = MHAT.Transform(ref array);
        //     array2 = MHAT.Syntes(ref wave, len);

        //     D2DVisualizer.DrawGraphic(ref gr, ref array2, new RectangleF(new PointF(25, 100 +pictureBox1.Height/2), new SizeF(pictureBox1.Width - 50, pictureBox1.Height/2 - 100)), style);
        //     D2DVisualizer.DrawSignature(ref gr, ref array2, new RectangleF(new PointF(25, 100 + pictureBox1.Height / 2), new SizeF(pictureBox1.Width - 50, pictureBox1.Height / 2 - 100)), style, new Size(25, 15));


        //     pictureBox1.Image = img;
        // }

        // private void button3_Click(object sender, EventArgs e)
        // {
        //     Numeric1DArray array = null;
        //     ArrayGenerator.Generate(Function.Exp, ref array, new Parameters(1000,1.0, -1, Math.PI));
        //     ArrayGenerator.Generate(@"series\usd.txt", ref array);

        //     Image img = pictureBox1.Image;
        //     System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(img);
        //     DrawStyle style = new DrawStyle(Color.Blue, new Point(0, 0), new Point(1, 1), Color.Red, Color.Green);
        //     D2DVisualizer.DrawGraphic(ref gr, ref array, new RectangleF(new PointF(25, 25), new SizeF(pictureBox1.Width - 50, pictureBox1.Height/2 - 100)), style);
        //     D2DVisualizer.DrawSignature(ref gr, ref array, new RectangleF(new PointF(25, 25), new SizeF(pictureBox1.Width - 50, pictureBox1.Height/2 - 100)), style,new Size(20,15));
        //     Numeric2DArray wave = MHAT.Analys(ref array, 100, 100, false, false);


        //     IsolineVisualizer.DrawIsoline(ref gr, new RectangleF(new PointF(25, 25 + pictureBox1.Height / 2), new SizeF(pictureBox1.Width - 50, pictureBox1.Height / 2 - 100)), ref wave, 52);
        //     //ref gr, ref wave, new RectangleF(new PointF(25, 25 + pictureBox1.Height/2), new SizeF(pictureBox1.Width - 50, pictureBox1.Height / 2 - 100)));
        //     IsolineVisualizer.DrawSignature(ref gr, ref wave, new RectangleF(new PointF(25, 25 + pictureBox1.Height / 2), new SizeF(pictureBox1.Width - 50, pictureBox1.Height / 2 - 100)), style, new Size(20, 15));

        //     pictureBox1.Image = img;
        // }

        // private void button4_Click(object sender, EventArgs e)
        // {
        //     /*ArrayDesigner arDesigner = new ArrayDesigner();
        //     if (arDesigner.ShowDialog() == DialogResult.OK)
        //     {
        //         Numeric1DArray array = new Numeric1DArray(arDesigner.GetArray());

        //         Image img = pictureBox1.Image;                
        //         System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(img);
        //         gr.Clear(Color.White);
        //         DrawStyle style = new DrawStyle(Color.Blue, new Point(0, 0), new Point(1, 1), Color.Red, Color.Green);
        //         D2DVisualizer.DrawGraphic(ref gr, ref array, new RectangleF(new PointF(25, 50), new SizeF(pictureBox1.Width - 50, pictureBox1.Height / 2 - 100)), style);
        //         D2DVisualizer.DrawSignature(ref gr, ref array, new RectangleF(new PointF(25, 50), new SizeF(pictureBox1.Width - 50, pictureBox1.Height / 2 - 100)), style, new Size(15, 15));


        //         Numeric2DArray wave = MHAT.Analys(ref array, 500, 500, true, false);
        //         Numeric2DArray scale = Analitics.Scalogramma(ref wave);
        //         Graphics.D3D.ColorVisualizerNonStatic col = new Diplom.Graphics.D3D.ColorVisualizerNonStatic(52);
        //         col.DrawGradientPaint(ref gr, ref wave, new RectangleF(new PointF(25, 100 + pictureBox1.Height / 2), new SizeF(pictureBox1.Width - 50, pictureBox1.Height / 2 - 100)));
        //         //Graphics.D3D.IsolineVisualizer.DrawSignature(ref gr, ref wave, new RectangleF(new PointF(25, 100 + pictureBox1.Height / 2), new SizeF(pictureBox1.Width - 50, pictureBox1.Height / 2 - 100)), style, new Size(20, 15));

        //         pictureBox1.Image = img;
        //     }
        //     ArrayLoadFromFile arrayLoadFromFile = new ArrayLoadFromFile();
        //     if (arrayLoadFromFile.ShowDialog() == DialogResult.OK)
        //     {
        //         Numeric1DArray array = new Numeric1DArray(arrayLoadFromFile.GetArray());

        //         Image img = pictureBox1.Image;                
        //         System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(img);
        //         gr.Clear(Color.White);
        //         DrawStyle style = new DrawStyle(Color.Blue, new Point(0, 0), new Point(1, 1), Color.Red, Color.Green);
        //         D2DVisualizer.DrawGraphic(ref gr, ref array, new RectangleF(new PointF(25, 50), new SizeF(pictureBox1.Width - 50, pictureBox1.Height / 2 - 100)), style);
        //         D2DVisualizer.DrawSignature(ref gr, ref array, new RectangleF(new PointF(25, 50), new SizeF(pictureBox1.Width - 50, pictureBox1.Height / 2 - 100)), style, new Size(15, 15));


        //         Numeric2DArray wave = MHAT.Analys(ref array, 250, 250, true, false);
        //         Numeric2DArray scale = Analitics.Scalogramma(ref wave);
        //         //Graphics.D3D.ColorVisualizerNonStatic col = new Diplom.Graphics.D3D.ColorVisualizerNonStatic(52);
        //         //col.DrawGradientPaint(ref gr, ref wave, new RectangleF(new PointF(25, 25 + pictureBox1.Height / 2), new SizeF(pictureBox1.Width - 50, pictureBox1.Height / 2 - 100)));
        //         //Graphics.D3D.IsolineVisualizer.DrawSignature(ref gr, ref wave, new RectangleF(new PointF(25, 100 + pictureBox1.Height / 2), new SizeF(pictureBox1.Width - 50, pictureBox1.Height / 2 - 100)), style, new Size(20, 15));
        //         Graphics.D3D.IsolineVisualizer.DrawIsoline(ref gr,
        //             new RectangleF(new PointF(25, 25 + pictureBox1.Height / 2), new SizeF(pictureBox1.Width - 50, pictureBox1.Height / 2 - 100)),
        //             ref wave, 52);
        //         IsolineVisualizer.DrawSignature(ref gr, ref wave, new RectangleF(new PointF(25, 25 + pictureBox1.Height / 2), new SizeF(pictureBox1.Width - 50, pictureBox1.Height / 2 - 100)), style, new Size(20, 15));

        //         pictureBox1.Image = img;
        //     }*/
        //     ArraySelectTypeOfLoad arraySelectTypeOfLoad = new ArraySelectTypeOfLoad();
        //     if (arraySelectTypeOfLoad.ShowDialog() == DialogResult.OK)
        //     {
        //         Numeric1DArray array = new Numeric1DArray(arraySelectTypeOfLoad.GetArray());
        //         //Filter.Denoise(ref array, 10, 4);

        //         array.TimeMin = 1997;
        //         array.TimeMax = 2010;


        //         Image img = pictureBox1.Image;
        //         System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(img);
        //         gr.Clear(Color.White);
        //         DrawStyle style = new DrawStyle(Color.Black, new Point(0, 0), new Point(1, 1), Color.Red, Color.Red);
        //         D2DVisualizer.DrawGraphic(ref gr, ref array, new RectangleF(new PointF(25, 25), new SizeF(pictureBox1.Width - 50, pictureBox1.Height / 3 - 50)), style);
        //         D2DVisualizer.DrawSignature(ref gr, ref array, new RectangleF(new PointF(25, 25), new SizeF(pictureBox1.Width - 50, pictureBox1.Height / 3 - 50)), style, new Size(14, 5));

        //         /*array.InitCharacters();
        //         for (int i = 0; i < array.Length; i++)
        //         {
        //             array.Insert(i, array.Items[i] - array.Average);
        //         }
        //         array.Min -= array.Average;
        //         array.Max -= array.Average;*/

        //         Numeric2DArray wave = MHAT.Analys(ref array, 200, 200, false, false);


        //         //Graphics.D3D.ColorVisualizerNonStatic col = new Diplom.Graphics.D3D.ColorVisualizerNonStatic(52);
        //         //col.DrawGradientPaint(ref gr, ref wave, new RectangleF(new PointF(25, 25 + pictureBox1.Height / 3), new SizeF(pictureBox1.Width - 50, pictureBox1.Height *2 / 3 - 50)));
        //         ColorVisualizer.DrawGraphic(ref gr, new RectangleF(new PointF(25, 25 + pictureBox1.Height / 3), new SizeF(pictureBox1.Width - 50, pictureBox1.Height * 2 / 3 - 50)), ref wave,52);
        //         //Graphics.D3D.IsolineVisualizer.DrawSignature(ref gr, ref wave, new RectangleF(new PointF(25, 100 + pictureBox1.Height / 2), new SizeF(pictureBox1.Width - 50, pictureBox1.Height / 2 - 100)), style, new Size(20, 15));
        //         /*Graphics.D3D.IsolineVisualizer.DrawIsoline(ref gr,
        //             new RectangleF(new PointF(25, 25 + pictureBox1.Height / 2), new SizeF(pictureBox1.Width - 50, pictureBox1.Height / 2 - 100)),
        //             ref wave, 52);
        //         IsolineVisualizer.DrawSignature(ref gr, ref wave, new RectangleF(new PointF(25, 25 + pictureBox1.Height / 2), new SizeF(pictureBox1.Width - 50, pictureBox1.Height / 2 - 100)), style, new Size(20, 15));
        //         */
        //         img.Save("res.Jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
        //         pictureBox1.Image = img;
        //     }
        // }

        ///* private void button5_Click(object sender, EventArgs e)
        // {
        //     FilterDesigner filterDesigner = new FilterDesigner();
        //     filterDesigner.Show();
        // }*/ 
        #endregion

        private void MainForm_Load(object sender, EventArgs e)
        {
            waveletPicture.Image = new Bitmap(waveletPicture.Width, waveletPicture.Height);
            skalogrammaPicture.Image = new Bitmap(skalogrammaPicture.Width, skalogrammaPicture.Height);
            skeylogrammaPicture.Image = new Bitmap(skeylogrammaPicture.Width, skeylogrammaPicture.Height);
            skeletonPicture.Image = new Bitmap(skeletonPicture.Width, skeletonPicture.Height);

            waveletType.SelectedIndex = 0;
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void CustomPaint()
        {
            if (tabControl.SelectedIndex == 0)
            {
                if (array1D != null)
                {
                    graphics = System.Drawing.Graphics.FromImage(waveletPicture.Image);
                    graphics.Clear(Color.White);
                    drawStyle = new DrawStyle(Color.Blue, new Point(0, 0), new Point(1, 1), Color.Red, Color.Green);

                    D2DVisualizer.DrawGraphic(ref graphics, ref array1D, new RectangleF(new PointF(40, 50), new SizeF(waveletPicture.Image.Width - 50, waveletPicture.Image.Height / 3 - 100)), drawStyle);
                    D2DVisualizer.DrawSignature(ref graphics, ref array1D, new RectangleF(new PointF(40, 50), new SizeF(waveletPicture.Image.Width - 50, waveletPicture.Image.Height / 3 - 100)), drawStyle, new Size(25, 10));

                    if (array2D != null)
                    {
                        if (isolineRadioButton.Checked)
                        {
                            IsolineVisualizer.DrawIsoline(ref graphics,
                                new RectangleF(new PointF(40, 25 + waveletPicture.Height / 3), new SizeF(waveletPicture.Width - 50, waveletPicture.Height * 2 / 3 - 50)),
                                ref array2D, (int)isolineCountNumericUpDown.Value);
                            if (black_whiteCheckBox.Checked)
                            {
                                graphics.Dispose();
                                Bitmap bmp = new Bitmap(waveletPicture.Image);
                                for (int i = 40; i < waveletPicture.Width - 10; i++)
                                {
                                    for (int j = 25 + waveletPicture.Height / 3; j < waveletPicture.Height * 2 / 3 - 25 + waveletPicture.Height / 3; j++)
                                    {
                                        Color rgb = bmp.GetPixel(i, j);
                                        int r = (int)(rgb.R * 0.3);
                                        int g = (int)(rgb.G * 0.59);
                                        int b = (int)(rgb.B * 0.11);
                                        rgb = Color.FromArgb(r + g + b, r + g + b, r + g + b);
                                        bmp.SetPixel(i, j, rgb);
                                    }
                                }
                                waveletPicture.Image = (Image)bmp;
                                graphics = System.Drawing.Graphics.FromImage(waveletPicture.Image);
                            }

                            IsolineVisualizer.DrawSignature(ref graphics, ref array2D, new RectangleF(new PointF(40, 25 + waveletPicture.Height / 3), new SizeF(waveletPicture.Width - 50, waveletPicture.Height * 2 / 3 - 50)), drawStyle, new Size(20, 10));
                        }
                        else
                        {
                            ColorVisualizer.DrawGraphic(ref graphics, new RectangleF(new PointF(40, 25 + waveletPicture.Height / 3), new SizeF(waveletPicture.Width - 50, waveletPicture.Height * 2 / 3 - 50)), ref array2D, (int)isolineCountNumericUpDown.Value);
                            if (black_whiteCheckBox.Checked)
                            {
                                graphics.Dispose();
                                Bitmap bmp = new Bitmap(waveletPicture.Image);
                                for (int i = 40; i < waveletPicture.Width - 10; i++)
                                {
                                    for (int j = 25 + waveletPicture.Height / 3; j < waveletPicture.Height * 2 / 3 - 25 + waveletPicture.Height / 3; j++)
                                    {
                                        Color rgb = bmp.GetPixel(i, j);
                                        int r = (int)(rgb.R * 0.3);
                                        int g = (int)(rgb.G * 0.59);
                                        int b = (int)(rgb.B * 0.11);
                                        rgb = Color.FromArgb(r + g + b, r + g + b, r + g + b);
                                        bmp.SetPixel(i, j, rgb);
                                    }
                                }
                                waveletPicture.Image = (Image)bmp;
                                graphics = System.Drawing.Graphics.FromImage(waveletPicture.Image);
                            }

                            IsolineVisualizer.DrawSignature(ref graphics, ref array2D, new RectangleF(new PointF(40, 25 + waveletPicture.Height / 3), new SizeF(waveletPicture.Width - 50, waveletPicture.Height * 2 / 3 - 50)), drawStyle, new Size(20, 10));
                        }                        
                    }
                    tabControl.Refresh();
                }
            }
            else if (tabControl.SelectedIndex == 1) // скалограмма
            {
                if (array1D != null)
                {
                    graphics = System.Drawing.Graphics.FromImage(skalogrammaPicture.Image);
                    graphics.Clear(Color.White);
                    drawStyle = new DrawStyle(Color.Blue, new Point(0, 0), new Point(1, 1), Color.Red, Color.Green);

                    D2DVisualizer.DrawGraphic(ref graphics, ref array1D, new RectangleF(new PointF(40, 50), new SizeF(skalogrammaPicture.Image.Width - 50, skalogrammaPicture.Image.Height / 3 - 100)), drawStyle);
                    D2DVisualizer.DrawSignature(ref graphics, ref array1D, new RectangleF(new PointF(40, 50), new SizeF(skalogrammaPicture.Image.Width - 50, skalogrammaPicture.Image.Height / 3 - 100)), drawStyle, new Size(25, 10));

                    if (array2D != null)
                    {
                        if (skalogramma == null)
                        {
                            skalogramma = new Numeric2DArray(0);
                            skalogramma = Analitics.Scalogramma(ref array2D);
                        }
                        if (isolineRadioButton.Checked)
                        {
                            IsolineVisualizer.DrawIsoline(ref graphics,
                                new RectangleF(new PointF(40, 25 + skalogrammaPicture.Height / 3), new SizeF(skalogrammaPicture.Width - 50, skalogrammaPicture.Height * 2 / 3 - 50)),
                                ref skalogramma, (int)isolineCountNumericUpDown.Value);
                            IsolineVisualizer.DrawSignature(ref graphics, ref skalogramma, new RectangleF(new PointF(40, 25 + skalogrammaPicture.Height / 3), new SizeF(skalogrammaPicture.Width - 50, skalogrammaPicture.Height * 2 / 3 - 50)), drawStyle, new Size(20, 10));
                        }
                        else
                        {
                            ColorVisualizer.DrawGraphic(ref graphics, new RectangleF(new PointF(40, 25 + skalogrammaPicture.Height / 3), new SizeF(skalogrammaPicture.Width - 50, skalogrammaPicture.Height * 2 / 3 - 50)), ref skalogramma, (int)isolineCountNumericUpDown.Value);
                            IsolineVisualizer.DrawSignature(ref graphics, ref skalogramma, new RectangleF(new PointF(40, 25 + skalogrammaPicture.Height / 3), new SizeF(skalogrammaPicture.Width - 50, skalogrammaPicture.Height * 2 / 3 - 50)), drawStyle, new Size(20, 10));
                        }                        
                    }
                    tabControl.Refresh();
                }
            }
            else if (tabControl.SelectedIndex == 2) // скейлограмма
            {
                if (array1D != null)
                {
                    graphics = System.Drawing.Graphics.FromImage(skeylogrammaPicture.Image);
                    graphics.Clear(Color.White);
                    drawStyle = new DrawStyle(Color.Blue, new Point(0, 0), new Point(1, 1), Color.Red, Color.Green);

                    D2DVisualizer.DrawGraphic(ref graphics, ref array1D, new RectangleF(new PointF(40, 50), new SizeF(skeylogrammaPicture.Image.Width - 50, skeylogrammaPicture.Image.Height / 3 - 100)), drawStyle);
                    D2DVisualizer.DrawSignature(ref graphics, ref array1D, new RectangleF(new PointF(40, 50), new SizeF(skeylogrammaPicture.Image.Width - 50, skeylogrammaPicture.Image.Height / 3 - 100)), drawStyle, new Size(25, 10));

                    if (array2D != null)
                    {
                        if (skeylogramma == null)
                        {
                            skeylogramma = new Numeric1DArray(0);
                            skeylogramma = Analitics.Sceylogramma(ref array2D, true);
                        }

                        D2DVisualizer.DrawGraphic(ref graphics, ref skeylogramma, new RectangleF(new PointF(40, 25 + skeylogrammaPicture.Height / 3), new SizeF(skeylogrammaPicture.Width - 50, skeylogrammaPicture.Height * 2 / 3 - 50)), drawStyle);
                        D2DVisualizer.DrawSignature(ref graphics, ref skeylogramma, new RectangleF(new PointF(40, 25 + skeylogrammaPicture.Height / 3), new SizeF(skeylogrammaPicture.Width - 50, skeylogrammaPicture.Height * 2 / 3 - 50)), drawStyle, new Size(25, 10));
                        
                        tabControl.Refresh();
                    }
                }
            }
            else if (tabControl.SelectedIndex == 3) // скелетон
            {
                if (array1D != null)
                {
                    graphics = System.Drawing.Graphics.FromImage(skeletonPicture.Image);
                    graphics.Clear(Color.White);
                    drawStyle = new DrawStyle(Color.Blue, new Point(0, 0), new Point(1, 1), Color.Red, Color.Green);

                    D2DVisualizer.DrawGraphic(ref graphics, ref array1D, new RectangleF(new PointF(40, 50), new SizeF(skeletonPicture.Image.Width - 50, skeletonPicture.Image.Height / 3 - 100)), drawStyle);
                    D2DVisualizer.DrawSignature(ref graphics, ref array1D, new RectangleF(new PointF(40, 50), new SizeF(skeletonPicture.Image.Width - 50, skeletonPicture.Image.Height / 3 - 100)), drawStyle, new Size(25, 10));

                    if (array2D != null)
                    {
                        if (skeleton == null)
                        {
                            skalogramma = new Numeric2DArray(0);
                            skalogramma = Analitics.Scalogramma(ref array2D);
                            skeleton = new Numeric2DArray(0);
                            skeleton = Analitics.Sceleton(ref skalogramma);
                        }

                        if (isolineRadioButton.Checked)
                        {
                            IsolineVisualizer.DrawIsoline(ref graphics,
                                new RectangleF(new PointF(40, 25 + skeletonPicture.Height / 3), new SizeF(skeletonPicture.Width - 50, skeletonPicture.Height * 2 / 3 - 50)),
                                ref skeleton, (int)isolineCountNumericUpDown.Value);
                            IsolineVisualizer.DrawSignature(ref graphics, ref array2D, new RectangleF(new PointF(40, 25 + skeletonPicture.Height / 3), new SizeF(skeletonPicture.Width - 50, skeletonPicture.Height * 2 / 3 - 50)), drawStyle, new Size(20, 10));
                        }
                        else
                        {
                            ColorVisualizer.DrawGraphic(ref graphics, new RectangleF(new PointF(40, 25 + skeletonPicture.Height / 3), new SizeF(skeletonPicture.Width - 50, skeletonPicture.Height * 2 / 3 - 50)), ref skeleton, (int)isolineCountNumericUpDown.Value);
                            IsolineVisualizer.DrawSignature(ref graphics, ref array2D, new RectangleF(new PointF(40, 25 + skeletonPicture.Height / 3), new SizeF(skeletonPicture.Width - 50, skeletonPicture.Height * 2 / 3 - 50)), drawStyle, new Size(20, 10));
                        }
                    }
                    tabControl.Refresh();
                }
            }
        }

        private void butGenerate_Click(object sender, EventArgs e)
        {
            ArrayDesigner arrayDesigner = new ArrayDesigner();
            if (arrayDesigner.ShowDialog() == DialogResult.OK)
            {
                array1D = new Numeric1DArray(arrayDesigner.Array);
                array1D.InitCharacters();
                array2D = null;
                skalogramma = null;
                skeylogramma = null;
                skeleton = null;
                GC.Collect();

                this.CustomPaint();
                groupBox3.Enabled = false;
            }
        }

        private void butApplyWavelet_Click(object sender, EventArgs e)
        {
            if (array1D == null || array1D.Length < 1)
            {
                return;
            }
            switch (waveletType.SelectedIndex)
            {
                case 0: // MHAT wavelet
                    {
                        array2D = null;
                        Thread thread = new Thread(new ThreadStart(waitForm));
                        thread.Start();
                        
                        array2D = MHAT.Analys(ref array1D, int.Parse(this.NAtextBox.Text), int.Parse(this.NBtextBox.Text), false, false);

                        thread.Abort();
                        thread = null;
                    } break;
                case 1: // Morlet wavelet
                    {
                        array2D = null;
                        Thread thread = new Thread(new ThreadStart(waitForm));
                        thread.Start();

                        array2D = Morlet.Analys(ref array1D, int.Parse(this.NAtextBox.Text), int.Parse(this.NBtextBox.Text), false, false);

                        thread.Abort();
                        thread = null;
                    } break;
            }
            if (array2D != null)
            {
                double twoPower = Math.Pow(2, array2D.MaxFreak);
                leftA.Minimum = rightA.Minimum = (decimal)array2D.MinFreak;
                leftA.Maximum = rightA.Maximum = (decimal)twoPower;
                leftA.Value = 1;
                rightA.Value = rightA.Maximum;
                leftB.Minimum = rightB.Minimum = (decimal)array2D.MinTime;
                leftB.Maximum = rightB.Maximum = (decimal)array2D.MaxTime;
                leftB.Value = 1;
                rightB.Value = rightB.Maximum;
                groupBox3.Enabled = true;
            }
            this.CustomPaint();
        }

        private void waitForm()
        {
            Wait w = new Wait();
            w.ShowDialog();
        }

        private void MainForm_Paint(object sender, PaintEventArgs e)
        {
            //this.CustomPaint();
        }

        private void isolineRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            this.CustomPaint();
        }

        private void NAtextBox_TextChanged(object sender, EventArgs e)
        {
            this.NAtextBox.Text = Regex.Replace(this.NAtextBox.Text, @"\D", "");
        }

        private void NBtextBox_TextChanged(object sender, EventArgs e)
        {
            this.NBtextBox.Text = Regex.Replace(this.NBtextBox.Text, @"\D", "");
        }

        private void butFilter_Click(object sender, EventArgs e)
        {
            FilterDesigner filterDesigner = new FilterDesigner();
            filterDesigner.Array = array1D;
            if (filterDesigner.ShowDialog() == DialogResult.OK)
            {
                array1D = new Numeric1DArray(filterDesigner.Array);
                array2D = null;
            }
            this.CustomPaint();
        }

        private void tabControl_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.CustomPaint();
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.InitialDirectory = Path.GetDirectoryName(Application.ExecutablePath);
            saveFileDialog.Filter = "Jpg files(*.jpg)|*.jpg";
            switch (tabControl.SelectedIndex)
            {
                case 0:
                    {
                        if (waveletPicture == null || waveletPicture.Image == null) { return; }
                        if (saveFileDialog.ShowDialog() == DialogResult.OK)
                        {
                            if (String.IsNullOrEmpty(saveFileDialog.FileName)) { return; }

                            waveletPicture.Image.Save(saveFileDialog.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
                            MessageBox.Show("Файл '" + saveFileDialog.FileName + "' сохранен", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    } break;
                case 1:
                    {
                        if (skalogrammaPicture == null || skalogrammaPicture.Image == null) { return; }
                        if (saveFileDialog.ShowDialog() == DialogResult.OK)
                        {
                            if (String.IsNullOrEmpty(saveFileDialog.FileName)) { return; }

                            skalogrammaPicture.Image.Save(saveFileDialog.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
                            MessageBox.Show("Файл '" + saveFileDialog.FileName + "' сохранен", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    } break;
                case 2:
                    {
                        if (skeylogrammaPicture == null || skeylogrammaPicture.Image == null) { return; }
                        if (saveFileDialog.ShowDialog() == DialogResult.OK)
                        {
                            if (String.IsNullOrEmpty(saveFileDialog.FileName)) { return; }

                            skeylogrammaPicture.Image.Save(saveFileDialog.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
                            MessageBox.Show("Файл '" + saveFileDialog.FileName + "' сохранен", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    } break;
                case 3:
                    {
                        if (skeletonPicture == null || skeletonPicture.Image == null) { return; }
                        if (saveFileDialog.ShowDialog() == DialogResult.OK)
                        {
                            if (String.IsNullOrEmpty(saveFileDialog.FileName)) { return; }

                            skeletonPicture.Image.Save(saveFileDialog.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
                            MessageBox.Show("Файл '" + saveFileDialog.FileName + "' сохранен", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    } break;
            }
        }

        private void leftA_ValueChanged(object sender, EventArgs e)
        {
            if (leftA.Value >= rightA.Value)
            {
                if (rightA.Value >= rightA.Maximum)
                {
                    rightA.Value = rightA.Maximum;
                    leftA.Value = rightA.Value - 1;
                }
            }
        }

        private void rightA_ValueChanged(object sender, EventArgs e)
        {
            if (rightA.Value <= leftA.Value)
            {
                if (leftA.Value <= leftA.Minimum)
                {
                    leftA.Value = leftA.Minimum;
                    rightA.Value = leftA.Value + 1;
                }
            }
        }

        private void leftB_ValueChanged(object sender, EventArgs e)
        {
            if (leftB.Value >= rightB.Value)
            {
                if (rightB.Value >= rightB.Maximum)
                {
                    rightB.Value = rightB.Maximum;
                    leftB.Value = rightB.Value - 1;
                }
            }
        }

        private void rightB_ValueChanged(object sender, EventArgs e)
        {
            if (rightB.Value <= leftB.Value)
            {
                if (leftB.Value <= leftB.Minimum)
                {
                    leftB.Value = leftB.Minimum;
                    rightB.Value = leftB.Value + 1;
                }
            }
        }

        private void applyScale_Click(object sender, EventArgs e)
        {
            if (array1D == null || array2D == null) return;

            PointF location = new PointF((float)leftB.Value, (float)leftA.Value);
            SizeF size = new SizeF((float)(rightB.Value - leftB.Value), (float)(rightA.Value - leftA.Value));

            Thread thread = new Thread(new ThreadStart(waitForm));
            thread.Start();
            switch (waveletType.SelectedIndex)
            {
                case 0: // MHAT wavelet
                    {
                        array2D = MHAT.Analys(ref array1D,
                            int.Parse(this.NAtextBox.Text), int.Parse(this.NBtextBox.Text), false, false,
                            new RectangleF(location, size));
                    } break;
                case 1: // Morle wavelet
                    {

                        array2D = Morlet.Analys(ref array1D,
                            int.Parse(this.NAtextBox.Text), int.Parse(this.NBtextBox.Text), false, false,
                            new RectangleF(location, size));
                    } break;
            }

            thread.Abort();
            thread = null;
            skalogramma = null;
            skeylogramma = null;
            skeleton = null;
            this.CustomPaint();
        }

        private void waveletsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WaveletsDesigner waveletsDesigner = new WaveletsDesigner();
            waveletsDesigner.ShowDialog();
        }

        private void fourierToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FourierDesigner fourierDesigner = new FourierDesigner(ref array1D);
            fourierDesigner.Show();
        }

        private void saveWithImageSaverToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (array1D != null && array2D != null)
            {
                Numeric1DArray temp = new Numeric1DArray(0);
                ImageSaver imageSaver = new ImageSaver(ref array1D, ref array2D, ref temp, ImageSaver.SaveType.Wavelet);
                imageSaver.ShowDialog();
            }
            else
            {
                MessageBox.Show("Необходимо сгенерировать ряд и применить вейвлет", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void MainForm_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Control && e.KeyCode == Keys.G)
            {
                groupBox4.Visible = !groupBox4.Visible;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (array1D != null)
            {
                array1D.TimeMin = int.Parse(textBox1.Text);
                array1D.TimeMax = int.Parse(textBox2.Text);
            }
            butApplyWavelet_Click(sender, e);
        }

        private void aboutUsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            About about = new About();
            about.ShowDialog();
        }
    }
}
